<?php
namespace Admin\Controller;
use Think\Controller;
class OrdersController  extends IndexController {
    public function lists(){

        $this->display();
    }
    public function get_order_list(){
                $orders=D("Order_info");
                $goodsUser=D("Goods_user");
                $goods=D("Goods_base");
                $goodsClass=D("Goods_class");
                $shopBase=D("Shop_base");
               $manager=D("Manager");
                if(I("request.iDisplayStart")==''){

                    $start=0;
                }else{
                    $start=I("request.iDisplayStart");
                }
                if(I("request.sSearch")!=''){//传过来的查询条件
                    $search['order_no'] = array('like','%'.I("request.sSearch").'%');
                }else{
                    $search='';
                }

                $res=$manager->where("username='".$_SESSION['name']."'")->find();
                if($_SESSION['name']=='laoxia'||$_SESSION['name']=="admin"){
                    //如查管理员可以查询搜索。
                    $where=$search;
                }else{
                    $where='shop_id='.$res['shop_id']." and lid!=3";
                }
                 $result=$orders->where($where)
                     ->order("id desc")
                     ->limit($start,I("request.iDisplayLength"))
                     ->select();//取order_info表的数据
                 $count=$orders->where($where)->count();
              //先现数据放入空数据，然后再根据语言情况进行进一步处理
                 $tmpArr=array();
                 foreach($result as $key=>$value){
                     $tmpArr[$key]=$value;

                     $tmpArr[$key]['create_time']=$value['create_time']!='' ? date('Y-m-d/H:i:s',$value['create_time']):'';
                 }
                //当语言为英文时执行如下操作
                 if( $_COOKIE["think_language"]=='en-us' ){
                    $arr=array();
                     foreach($tmpArr as $kk=>$vv){
                         $arr[$kk]=$vv;
                         $userResult= $goodsUser->where("uid=".$vv['uid'])->getField('en_name');
                         $arr[$kk]['uid']=$userResult;

                         $vv['goods_ids']=rtrim($vv['goods_ids'],',');//截取商品id字符串数据去掉最后边的
                         $ids=explode(',',$vv['goods_ids']); //把字符串转成数组，用于where条件的in条件
                         $str="";
                         $map['id']=array('in',$ids);     //拼接where查询语句
                         $goodsResult=$goods->where($map)->select();
                         foreach($goodsResult as $kkk=>$vvv){
                             $str.=$vvv['en_name']."<br />";
                         }
                         $arr[$kk]['goods_ids']=$str;//把拼接好的商品名字符串赋给商品ID
                         $goodsClassResult= $goodsClass->where('id='.$vv['lid'])->getfield("en_name"); //查询商品的类别
                         $arr[$kk]['lid']= $goodsClassResult;
                         $shopResult=$shopBase->where("id=".$vv["shop_id"])->getfield('en_name'); //查询商户类型信息
                         $arr[$kk]['shop_id']=$shopResult;

                         if($vv['doctor_id']!=0){  //视频服务
                             $Doctor=D("Doctor");
                             $doctorInfo=$Doctor->where("id=".$vv['doctor_id'])->find();
                             if($vv['optimum_time']!=null){
                                 $shipin=date("Y-m-d H:i:s",$vv['optimum_time'])."到".date("Y-m-d H:i:s",$vv['end_time']);
                             }else{
                                 $shipin="没有确认时间";
                             }
                             $arr[$kk]['shipin']=  $doctorInfo['name']."<br/>".$shipin;
                             $arr[$kk]['is_doctor']=1;
                         }else{
                             $arr[$kk]['shipin']="null";
                             $arr[$kk]['is_doctor']=0;
                         }
                         if($_SESSION['name']=="laoxia"||$_SESSION['name']=="admin"){
                             $arr[$kk]['is_c']=1;
                         }else{
                             $arr[$kk]['is_c']=0;
                         }
                     }
                 }


            //当语言为英文时执行如下操作
            if( $_COOKIE["think_language"]=='zh-cn' ){
                $arr=array();
                foreach($tmpArr as $kk=>$vv){
                    $arr[$kk]=$vv;
                    $userResult= $goodsUser->where("uid=".$vv['uid'])->getField('name');
                    $arr[$kk]['uid']=$userResult;

                    $vv['goods_ids']=rtrim($vv['goods_ids'],',');//截取商品id字符串数据去掉最后边的
                    $ids=explode(',',$vv['goods_ids']); //把字符串转成数组，用于where条件的in条件
                    $str="";
                    $map['id']=array('in',$ids);     //拼接where查询语句
                    $goodsResult=$goods->where($map)->select();
                    foreach($goodsResult as $kkk=>$vvv){
                        $str.=$vvv['name']."<br />";
                    }
                    $arr[$kk]['goods_ids']=$str;//把拼接好的商品名字符串赋给商品ID
                    $goodsClassResult= $goodsClass->where('id='.$vv['lid'])->getfield("name"); //查询商品的类别
                    $arr[$kk]['lid']= $goodsClassResult;
                    $shopResult=$shopBase->where("id=".$vv["shop_id"])->getfield('name'); //查询商户类型信息
                    $arr[$kk]['shop_id']=$shopResult;

                    if($vv['doctor_id']!=0){  //视频服务
                        $Doctor=D("Doctor");
                        $doctorInfo=$Doctor->where("id=".$vv['doctor_id'])->find();
                        if($vv['optimum_time']!=null){
                            $shipin=date("Y-m-d H:i:s",$vv['optimum_time'])."到".date("Y-m-d H:i:s",$vv['end_time']);
                        }else{
                            $shipin="没有确认时间";
                        }
                        $arr[$kk]['shipin']=  $doctorInfo['name']."<br/>".$shipin;
                        $arr[$kk]['is_doctor']=1;
                    }else{
                        $arr[$kk]['shipin']="null";
                        $arr[$kk]['is_doctor']=0;
                    }
                    if($_SESSION['name']=="laoxia"||$_SESSION['name']=="admin"){
                        $arr[$kk]['is_c']=1;
                    }else{
                        $arr[$kk]['is_c']=0;
                    }
                }
            }


                    if($_SESSION['name']=="laoxia"||$_SESSION['name']=="admin"){
                         $arr[$key]['is_c']=1;
                     }else{
                         $arr[$key]['is_c']=0;
                     }
                 
                   if(!$result){
                    echo json_encode(0); exit;
                     }

        $ret = array(
            "sEcho" => intval($_GET['sEcho']),
            "iTotalRecords" => $start,
            "iTotalDisplayRecords" =>$count,
            "aaData" => $arr
        );

        echo json_encode($ret);exit;
            }




    public function confirm(){
            $orders=D("Order_info");
            $manager=D("Manager");
            $id=I("get.id");
            $status=I("get.status");
            $res=$orders->where("id=".$id)->find();
            if(!$res){
                echo json_encode(0); exit;
            }
            if($res['order_status']==0&&$res['pay_status']==1&&($status==1||$status==2)){
                $result=$orders->where("id=".$id)->save(array('order_status'=>$status,"update_time"=>time()));
                if(!$result){
                    echo json_encode(0); exit;
                }
                if($status==1){
                    $shopBase=D("shopBase");
                    $r1=$shopBase->where("id=".$res['shop_id'])->find();
                    if(!$r1){
                        echo json_encode(0); exit;
                    }
                    //扣手续费
                    //判断佣金种类
                    if($r1['percentage_id']>0){
                        $poundage_log=D("poundageLog");
                        
                        //判断佣金金额总和
                        $commission=D("Commission");
                        $results=$commission->where("id=".$r1['percentage_id'])->find();
                        if($results['is_one']==1){
                            $na_members=strtotime("+1 years",$r1['create_time']);  
                        }
                        if($na_members>time()){ //还没过1年,不扣佣金
                            $money=0;
                        }else{  //过了一年了
                            $arrs=unserialize($results['rule']);
                            $money=0;
                            $temp=key($arrs);
                            if('每单'===$temp){
                                $money=$arrs['每单'];
                            }elseif('每套'===$temp){

                                $money=$res['goods_total_price']*$arrs['每套'];
                            }else{
                                $keys=array_keys($arrs);


                                //查找本月有多少个商品id为6的
                                $firstday = date("Y-m-01",time());
                                $lastday = date("Y-m-d",strtotime("$firstday +1 month -1 day"));
                                $f=strtotime($firstday);
                                $l=strtotime($lastday);
                                //嗣道就是个坑
                                $where="create_time >$f and create_time<$l and shop_id=6";
                                $n=$orders->where($where)->select();
                                $count=0;
                                foreach($n as $v){
                                    if(in_array(54, explode(',', rtrim($v['goods_ids'],',')))){
                                        $count++;
                                    }
                                }

                                foreach(explode(',', rtrim($res['goods_ids'],',')) as $vv){
                                    if($keys[1]==$vv){
                                        if($count<=5){
                                            $money+=20000;
                                        }elseif($count>5&&$count<=15){
                                            $money+=24000;
                                        }elseif($count>15&&$count<=25){
                                            $money+=27000;
                                        }else{
                                            $money+=30000;
                                        }
                                    }else{
                                        $goodsBase=D("GoodsBase");
                                        $goods=$goodsBase->where("id=".$vv)->find();
                                        $money+=$goods['price']*0.1;
                                    }
                                }
                            }
                        }
                        
                        
                        //判断是否是视频订单还是普通订单
                        if($res['lid']==3&&$res['doctor_id']!=0){
                            $doctor=D("Doctor");
                            $rr=$doctor->where("id=".$res['doctor_id'])->setInc("money",$res['goods_total_price']);
                        }else{
                            $rr=$manager->where("shop_id=".$res['shop_id'])->setInc("money",$res['goods_total_price']-$money);
                        }
                        if(!$rr){
                           echo json_encode(0); exit; 
                        }

                        $rrr=$manager->where("shop_id=".$res['shop_id'])->find();
                        $data['uid']=$res['uid'];
                        $data['manager_id']=$rrr['id'];
                        if($res['lid']==3&&$res['doctor_id']!=0){
                            $data['doctor_id']=$res['doctor_id'];
                        }
                        $data['order_id']=$res['id'];
                        $data['money']=$money;
                        $data['action']="手续费";
                        $data['who']=$_SESSION['name'];
                        $data['log']="用户名为".$_SESSION['name']."的用户扣除了平台手续费".$money;
                        $data['create_time']=time();
                        $r2=$poundage_log->add($data);
                        if(!$r2){
                           echo json_encode(0); exit; 
                        }
                    }
                    echo json_encode(1); exit;
                }  
            }
            echo json_encode(0); exit; 
            
            
    }
        public function goods_user($order){
        $orders=D("Order_info");
        $goods_user=D("GoodsUser");
        $res=$orders->where("order_no='".$order."'")->find();
        $r=$goods_user->where("id=".$res['addr_id'])->find();

            if( $_COOKIE["think_language"]=='en-us' ){
                if($r['garden']==0){
                    $r['garden']= 'male';

                }elseif($r['garden']==1){
                    $r['garden']= 'female';
                }

                if($r['papers_type']==0){
                    $r['papers_type']="ID Card";
                }else{
                    $r['papers_type']="passport";
                }

            }else{
                if($r['papers_type']==0){
                    $r['papers_type']=" 身份证";
                }else{
                    $r['papers_type']="护照 ";
                }

                if($r['garden']==0){
                    $r['garden']= '女';

                }elseif($r['garden']==1){
                    $r['garden']= '男';
                }

            }


        unset($r['is_order']);
        unset($r['uid']);
       $this->assign("res",$r);
       $this->display();
    }

    
    public function shipin($id,$type=0){
        $orders=D("Order_info");
        $Doctor=D("Doctor");
        $res=$orders->where("id=".$id)->find();
        if(!$res){
            echo "javascript:alert('错误!')";
        }
        if($type==1){
            $start_time=I("request.start_time");
            $end_time=I("request.end_time");
            $data['optimum_time']=strtotime($start_time);
            $data['end_time']=strtotime($end_time);
            $data['update_time']=time();
            $orders->where("id=".$id)->save($data);
            header("location:./index.php?m=Admin&c=Orders&a=lists");
        }
        $arr=array();
        $arr['id']=$id;
        $arr['start_time']=  date("Y-m-d H:i:s",$res['optimum_time']);
        $arr['end_time']=date("Y-m-d H:i:s",$res['end_time']);
        $rrr=$Doctor->where("id=".$res['doctor_id'])->find();
        $arr['name']=$rrr['name'];
        $this->assign("times",$arr);
        $this->display();
    }
}